package com.guangsi.compute;

import org.hyperic.sigar.CpuPerc;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;

public class CpuUtils {
    private static Sigar sigar = null;

    private CpuUtils() {
        try {
            sigar = new Sigar();
        } catch (Exception var2) {
            var2.printStackTrace();
        }

    }

    public static final CpuUtils getInstance() {
        return CpuUtils.CupCtlHolder.instance;
    }

    public Sigar getSigar() {
        return sigar;
    }

    public double getCpuUsage() {
        double used = 0.0D;

        try {
            CpuPerc[] cpuPercs = getInstance().getSigar().getCpuPercList();
            CpuPerc[] percs = cpuPercs;
            int length = cpuPercs.length;

            for(int i = 0; i < length; ++i) {
                CpuPerc perc = percs[i];
                if (!Double.isNaN(perc.getCombined())) {
                    used += perc.getCombined();
                }
            }

            used /= (double)cpuPercs.length;
            return used;
        } catch (SigarException var11) {
            var11.printStackTrace();
            return used;
        } finally {
            ;
        }
    }

    private static class CupCtlHolder {
        private static final CpuUtils instance = new CpuUtils();

        private CupCtlHolder() {
        }
    }
}